package xs.com.location.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2015/9/7.
 */
public class SortUtil {
    /**
     * key 升序
     *
     * @param maptest
     * @return
     */
    public static List<StringBuffer> mySort(HashMap<String, String> maptest) {
        List<StringBuffer> list = new ArrayList<StringBuffer>();
        List<String> list1 = new ArrayList<String>();
        StringBuffer str1 = new StringBuffer();
        StringBuffer str2 = new StringBuffer();

        Collection<String> keyset = maptest.keySet();
        list1 = new ArrayList<String>(keyset);

        //对key键值按字典升序排序
        Collections.sort(list1);
        str1.append("{");
        for (int i = 0; i < list1.size(); i++) {
            str1.append(list1.get(i) + ":\"" + maptest.get(list1.get(i)) + "\"");
            if (i == (list1.size() - 1)) {

                str1.append("");
            } else {
                str1.append(",");
            }
            str2.append(list1.get(i) + "=" + maptest.get(list1.get(i)));
            if (i == (list1.size() - 1)) {
                str2.append("");
            } else {
                str2.append("&");
            }
        }
        str1.append("}");
        list.add(str1);
        list.add(str2);
        System.out.println(str1);
        System.out.println(str2);
        return list;
    }

}
